﻿namespace Nello
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Reflection;

    public class DataUtil
    {
        public static DataTable DoCrossTableQuery(DataTable srcTable, DataTable destTable, string keyName, string destColName)
        {
            return DoCrossTableQuery(srcTable, destTable, keyName, keyName, destColName, destColName);
        }

        public static DataTable DoCrossTableQuery(DataTable srcTable, DataTable destTable, string srcKey, string destKey, string destColName)
        {
            return DoCrossTableQuery(srcTable, destTable, srcKey, destKey, destColName, destColName);
        }

        public static DataTable DoCrossTableQuery(DataTable srcTable, DataTable destTable, string srcKey, string destKey, string destColName, string newColName)
        {
            string[] ncNames = newColName.Split(",".ToCharArray());
            string[] dcNames = destColName.Split(",".ToCharArray());
            int count = ncNames.Length;
            int i = 0;
            while (i < count)
            {
                if (!srcTable.Columns.Contains(ncNames[i]))
                {
                    srcTable.Columns.Add(ncNames[i]);
                }
                i++;
            }
            foreach (DataRow row in srcTable.Rows)
            {
                DataRow[] rows = destTable.Select(string.Concat(new object[] { destKey, "='", row[srcKey], "'" }));
                if (rows.Length < 1)
                {
                    i = 0;
                    while (i < count)
                    {
                        row[ncNames[i]] = "";
                        i++;
                    }
                    continue;
                }
                for (i = 0; i < count; i++)
                {
                    row[ncNames[i]] = rows[0][dcNames[i]];
                }
            }
            return srcTable;
        }

        public static void ExchangeData(Hashtable ht, DataRow row)
        {
            ExchangeData(ht, row, ExchangeType.AllColumn);
        }

        public static void ExchangeData(DataRow row, Hashtable ht)
        {
            ExchangeData(row, ht, ExchangeType.MatchColumn);
        }

        public static void ExchangeData(DataRow row, object obj)
        {
            ExchangeData(row, obj, ExchangeType.MatchColumn);
        }

        public static void ExchangeData(object obj, DataRow row)
        {
            foreach (DataColumn col in row.Table.Columns)
            {
                foreach (PropertyInfo pi in obj.GetType().GetProperties())
                {
                    if (col.ColumnName.ToLower().Equals(pi.Name.ToLower()))
                    {
                        pi.SetValue(obj, Convert.ChangeType(row[col.ColumnName], pi.PropertyType), null);
                    }
                }
            }
        }

        public static void ExchangeData(Hashtable ht, DataRow row, ExchangeType et)
        {
            if (et == ExchangeType.AllColumn)
            {
                foreach (DataColumn col in row.Table.Columns)
                {
                    if (!ht.ContainsKey(col.ColumnName))
                    {
                        ht.Add(col.ColumnName, null);
                    }
                }
            }
            foreach (DataColumn col in row.Table.Columns)
            {
                if (ht.ContainsKey(col.ColumnName))
                {
                    ht[col.ColumnName] = row[col];
                }
            }
        }

        public static void ExchangeData(DataRow row, Hashtable ht, ExchangeType et)
        {
            if (et == ExchangeType.AllColumn)
            {
                foreach (object obj in ht.Keys)
                {
                    if (!row.Table.Columns.Contains(obj.ToString()))
                    {
                        row.Table.Columns.Add(obj.ToString());
                    }
                }
            }
            foreach (object obj in ht.Keys)
            {
                if (row.Table.Columns.Contains(obj.ToString()))
                {
                    row[obj.ToString()] = ht[obj];
                }
            }
        }

        public static void ExchangeData(DataRow row, object obj, ExchangeType et)
        {
            if (et == ExchangeType.AllColumn)
            {
                foreach (PropertyInfo pi in obj.GetType().GetProperties())
                {
                    if (!row.Table.Columns.Contains(pi.Name))
                    {
                        row.Table.Columns.Add(pi.Name);
                    }
                }
            }
            foreach (DataColumn col in row.Table.Columns)
            {
                foreach (PropertyInfo pi in obj.GetType().GetProperties())
                {
                    if (col.ColumnName.ToLower().Equals(pi.Name.ToLower()))
                    {
                        row[col.ColumnName] = pi.GetValue(obj, null).ToString();
                    }
                }
            }
        }

        public static ArrayList GetArrayList(DataColumn col)
        {
            ArrayList list = new ArrayList();
            foreach (DataRow row in col.Table.Rows)
            {
                list.Add(row[col].ToString());
            }
            return list;
        }

        public static ArrayList GetArrayList(DataRow row)
        {
            ArrayList list = new ArrayList();
            foreach (DataColumn col in row.Table.Columns)
            {
                list.Add(row[col].ToString());
            }
            return list;
        }

        public static DataTable GetDataTabePerPage(DataTable dt, int index, int size)
        {
            DataTable dtt = new DataTable();
            dtt = dt.Clone();
            int min = index * size;
            int max = (index + 1) * size;
            for (int i = min; i < max; i++)
            {
                if (i < dt.Rows.Count)
                {
                    dtt.ImportRow(dt.Rows[i]);
                }
            }
            return dtt;
        }

        public static DataTable GetDataTable(string columnNames)
        {
            DataTable dt = new DataTable();
            string[] names = columnNames.Split(",".ToCharArray());
            foreach (string name in names)
            {
                dt.Columns.Add(name);
            }
            return dt;
        }

        public static double GetTotals(DataTable dt, int colIndex)
        {
            string name = dt.Columns[colIndex].ColumnName;
            return GetTotals(dt, colIndex);
        }

        public static double GetTotals(DataTable dt, string colName)
        {
            double total = 0.0;
            double tmp = 0.0;
            foreach (DataRow row in dt.Rows)
            {
                double.TryParse(row[colName].ToString(), out tmp);
                total += tmp;
            }
            return total;
        }

        public static string Parse(Hashtable ht)
        {
            return StringUtil.Join((IDictionary) ht);
        }

        public static Hashtable Parse(string str)
        {
            Hashtable ht = new Hashtable();
            string[] ss = str.Split("|".ToCharArray());
            foreach (string s in ss)
            {
                string key = s.Split("=".ToCharArray())[0];
                string val = s.Substring(s.IndexOf("=") + 1);
                ht.Add(key, val);
            }
            return ht;
        }
    }
}

